﻿#ifndef SCENE_VIEWER_FUNCTIONS
#define SCENE_VIEWER_FUNCTIONS 1

#include <vector>
#include <string>
#include <iostream>
#include <osg/Matrix>
#include <osg/Vec4>
#include <Define.h>
#include <osg/Camera>
#include <osg/Geode>
#include <osgGA/GUIEventHandler>
#include <osgGA/CameraManipulator>
#include <SkyBox.h>
//#include <IdDistributeVisitor.h>
#include <osgSim/DOFTransform>
//#include <PathModel.h>
//#include <LuaScriptCallBack.h>

namespace SceneViewerFunctions
{
	//生成osg_vec4
	OSG_EDITOR_DLL osg::Vec4 osg_vec4(double r, double g, double b, double a);

	//osg_vec3相关
	OSG_EDITOR_DLL double osg_vec3_at(const osg::Vec3& vec, int index);
	OSG_EDITOR_DLL osg::Vec3 osg_vec3(double x, double y, double z);
	//osg_matrix相关
	OSG_EDITOR_DLL osg::Vec3 osg_matrix_getTrans(const osg::Matrix& m);
	OSG_EDITOR_DLL osg::Vec3 osg_matrix_getRotate(const osg::Matrix& m);
	OSG_EDITOR_DLL osg::Matrix osg_matrix_TransRotate(const osg::Vec3& trans, const osg::Vec3& rote);
	OSG_EDITOR_DLL osg::Matrix osg_matrix_RotateTrans(const osg::Vec3& rote , const osg::Vec3& trans );
    OSG_EDITOR_DLL osg::Matrix osg_matrix_ScaleRotateTrans(const osg::Vec3& scale, const osg::Vec3& rote , const osg::Vec3& trans );

	//modelBase相关
    //OSG_EDITOR_DLL void  setModelBaseMatrix(const std::string& modle_base_name, osg::Matrix m);
    //
    //OSG_EDITOR_DLL osg::Matrix  getModelBaseMatrix(const std::string& modle_base_name);
    //OSG_EDITOR_DLL void OnModelBaseByName(const std::string& model_name);
    //OSG_EDITOR_DLL void OffModelBaseByName(const std::string& model_name);
    //
    //OSG_EDITOR_DLL int getModelBaseSwitchState(const std::string& model_name);//得到当前modelbase的开关状态，1代表打开，0代表没打开，-1代表没有找到modelbase
    //OSG_EDITOR_DLL void enableModelBaseShowPolygon(const std::string& model_name, bool bool_value);
    //OSG_EDITOR_DLL std::string getModelBaseName(int index_of_vector);	//得到modelBase的name
    //OSG_EDITOR_DLL void setModelBaseColorByName(const std::string& model_name, const osg::Vec4& color);
    //OSG_EDITOR_DLL void setAllModelBaseColor(const osg::Vec4& color);
    //OSG_EDITOR_DLL void unSetModelBaseColorByName(const std::string& model_name);
    //OSG_EDITOR_DLL void unSetAllModelBaseColor();
    //OSG_EDITOR_DLL void sceneViewerSetUpPickHandler();
    //OSG_EDITOR_DLL void  creatGround(const std::string& modle_base_name, double size_x, double size_y);

    //传入矩阵向量，构建轨迹的几何体
    //OSG_EDITOR_DLL osg::ref_ptr<PathModel>  createPathModelFromMatrixVector(const std::string& name, const std::vector<osg::Matrix>& matrix_vector, double line_width = 2.0, osg::Vec4 color = osg::Vec4(1.0, 0.0, 1.0, 1.0), double duration_time = 3 );


    //链接两个modelBase的回调
    //OSG_EDITOR_DLL bool  connectModelMatrixCallBack(const std::string& name1, const std::string& name2);
	//测试轨迹矩阵通信功能
    //OSG_EDITOR_DLL void  testPathModel();
	
	//设置sceneViewer的notifyCallBack回调
    //OSG_EDITOR_DLL void  setUpSceneViewerNotifyCallBack();
    //OSG_EDITOR_DLL void  removeSceneViewerNotifyCallBack();
	
	//viewer的setUpSceneData
	OSG_EDITOR_DLL void  setUpSceneData();

	//增加默认trackBall相机操作器
	OSG_EDITOR_DLL void addDefaultTrackBallCameraManipulator(const std::string& name);
    OSG_EDITOR_DLL void addCameraManipulatorIntersection(const std::string& name);

	//设置viewer的初始化窗口：
	OSG_EDITOR_DLL void setUpViewInWindow(int x, int y, int width, int height);

	//从sceneviewer中获取相机
	OSG_EDITOR_DLL osg::Camera*  getOsgCamera();

	//给sceneViewer中添加osg子节点 #test#
	OSG_EDITOR_DLL void addOsgNodeToSceneViewer(osg::ref_ptr<osg::Node> node);
	OSG_EDITOR_DLL void sceneViewerAddNodeToModelRoot(osg::ref_ptr<osg::Node> node);

    //sceneViewer相关
    //OSG_EDITOR_DLL int getModelBaseVectorSize();//得到sceneviewer中modelbase向量的大小
	OSG_EDITOR_DLL osg::ref_ptr<osg::Node> sceneViewerInstance();

	//小坐标系
	OSG_EDITOR_DLL void addCoordinateToSceneViewer();
	osg::ref_ptr<osg::Group> CreateCoordinate(double scale);

	//camera manipulator相机相关
	OSG_EDITOR_DLL int getCameraVectorSize();
	OSG_EDITOR_DLL std::string getCameraManipulatorName(int index);
	OSG_EDITOR_DLL void cameraFitScene();
	OSG_EDITOR_DLL osg::ref_ptr<osgGA::CameraManipulator> sceneViewerGetCameraManipulator(int index);
	OSG_EDITOR_DLL osg::ref_ptr<osgGA::CameraManipulator> findCameraManipulatorByIdString(const std::string& id_string);


	//获取场景根节点
	OSG_EDITOR_DLL osg::ref_ptr<osg::Node> getSceneViewerRootNode();
	OSG_EDITOR_DLL osg::ref_ptr<osg::Node> getSceneViewerModelRootNode();

	//清理场景模型根节点
	OSG_EDITOR_DLL void clearSceneViewerModelRootNode();

	//父子节点信息获取相关函数
	OSG_EDITOR_DLL int getChildCount(osg::ref_ptr<osg::Node> node_ptr);
	OSG_EDITOR_DLL osg::ref_ptr<osg::Node> getChildOfNode(osg::ref_ptr<osg::Node> node_ptr, int index);
	OSG_EDITOR_DLL std::string getNodeName(osg::ref_ptr<osg::Node> node_ptr);


	//osgDB相关，读取模型
	OSG_EDITOR_DLL void readNodeFromFile(const std::string& file_name);
	OSG_EDITOR_DLL osg::ref_ptr<osg::Node> osgDBReadNodeFile(const std::string& file_name);
	OSG_EDITOR_DLL void writeNodeToFile(osg::ref_ptr<osg::Node> node_ptr, const std::string& file_name);

	//node节点相关
	OSG_EDITOR_DLL std::string osgNodeTypeName(osg::ref_ptr<osg::Node> node_ptr);
	OSG_EDITOR_DLL void nodeAddDescription(osg::ref_ptr<osg::Node> node_ptr, const std::string& des_string, const std::string& string_value);
	OSG_EDITOR_DLL osg::ref_ptr<osg::Node> findNodeByIdString(const std::string& id_string);
	OSG_EDITOR_DLL void setNodeColor(osg::ref_ptr<osg::Node> node_ptr, const osg::Vec4& color);
	OSG_EDITOR_DLL std::string nodeGetDescription(osg::ref_ptr<osg::Node> node_ptr, const std::string& des_string);
    //OSG_EDITOR_DLL void nodeDoLuaFiles(osg::ref_ptr<osg::Node> node_ptr);
	OSG_EDITOR_DLL bool nodePtrUsable(osg::ref_ptr<osg::Node> node_ptr);
	OSG_EDITOR_DLL void nodeAddChild(osg::ref_ptr<osg::Node> node_ptr, osg::ref_ptr<osg::Node> child_node_ptr);
	OSG_EDITOR_DLL void nodeSetUpdateCallBack(osg::ref_ptr<osg::Node> node_ptr, osg::ref_ptr<osg::NodeCallback> node_call_back);
	OSG_EDITOR_DLL void nodeInsertParent(osg::ref_ptr<osg::Node> node_ptr, osg::ref_ptr<osg::Node> parent_node_ptr);
	OSG_EDITOR_DLL osg::ref_ptr<osg::NodeCallback> nodeGetUpdateCallBack(osg::ref_ptr<osg::Node> node_ptr);
	OSG_EDITOR_DLL osg::ref_ptr<osg::Node> nodeGetParent(osg::ref_ptr<osg::Node> node_ptr);
	OSG_EDITOR_DLL void nodeSetNodeMask(osg::ref_ptr<osg::Node> node_ptr, int node_mask);
	OSG_EDITOR_DLL void nodeHide(osg::ref_ptr<osg::Node> node_ptr);
	OSG_EDITOR_DLL void nodeShow(osg::ref_ptr<osg::Node> node_ptr);
	OSG_EDITOR_DLL bool nodeIsShow(osg::ref_ptr<osg::Node> node_ptr);
	OSG_EDITOR_DLL void nodeAllShow();
	OSG_EDITOR_DLL void nodeAllHide();






	//eventHandler相关
	OSG_EDITOR_DLL int sceneViewerGetEventHandlerSize();
	OSG_EDITOR_DLL osg::ref_ptr<osgGA::GUIEventHandler> sceneViewerGetEventHandler(int index);
	OSG_EDITOR_DLL osg::ref_ptr<osg::Object> sceneViewerGetEventHandlerObject(int index);
	OSG_EDITOR_DLL osg::ref_ptr<osg::Object> GUIEventHandlerPrtToOsgObjectPtr(osg::ref_ptr<osgGA::GUIEventHandler> handler);
	OSG_EDITOR_DLL osg::ref_ptr<osgGA::GUIEventHandler> findGUIEventHandlerByIdString(const std::string& id_string);
	//osg::object相关
	OSG_EDITOR_DLL std::string osgObjectTypeName(osg::ref_ptr<osg::Object> obj_ptr);
	OSG_EDITOR_DLL void osgObjectAddDescription(osg::ref_ptr<osg::Object> obj_ptr, const std::string& des_string, const std::string& string_value);
	OSG_EDITOR_DLL std::string osgObjectGetDescription(osg::ref_ptr<osg::Object> obj_ptr, const std::string& des_string);
	OSG_EDITOR_DLL std::string osgObjectName(osg::ref_ptr<osg::Object> obj_ptr);
	OSG_EDITOR_DLL osg::ref_ptr<osg::Object> voidPtrToOsgObjectPtr(void* void_ptr);
	OSG_EDITOR_DLL void* osgObjectPtrToVoidPtr(osg::ref_ptr<osg::Object> obj_ptr);
	
	//EditorModeManager相关
	OSG_EDITOR_DLL void editorModeManagerSetEditingNode(osg::ref_ptr<osg::Node> editing_node);
	OSG_EDITOR_DLL void editorModeManagerEnterEditing();
	OSG_EDITOR_DLL void editorModeManagerQuitEditing();

	//geode相关
	OSG_EDITOR_DLL int geodeGetDrawableCount(osg::ref_ptr<osg::Geode> geode_ptr);
	OSG_EDITOR_DLL osg::ref_ptr<osg::Drawable> geodeGetDrawable(osg::ref_ptr<osg::Geode> geode_ptr, int index);
	OSG_EDITOR_DLL osg::ref_ptr<osg::Drawable> geodeFindDrawableById(osg::ref_ptr<osg::Geode> geode_ptr, const std::string& id_str);



    //geometry相关
	//OSG_EDITOR_DLL void geometry();

	//SkyBox相关
	OSG_EDITOR_DLL osg::ref_ptr<SkyBox> skyBoxNew();
	OSG_EDITOR_DLL void skyBoxSetBackImagePath(osg::ref_ptr<SkyBox> sky_box, const std::string& back_image);
	OSG_EDITOR_DLL void skyBoxSetFrontImagePath(osg::ref_ptr<SkyBox> sky_box, const std::string& front_image);
	OSG_EDITOR_DLL void skyBoxSetDownImagePath(osg::ref_ptr<SkyBox> sky_box, const std::string& down_image);
	OSG_EDITOR_DLL void skyBoxSetUpImagePath(osg::ref_ptr<SkyBox> sky_box, const std::string& up_image);
	OSG_EDITOR_DLL void skyBoxSetRightImagePath(osg::ref_ptr<SkyBox> sky_box, const std::string& right_image);
	OSG_EDITOR_DLL void skyBoxSetLeftImagePath(osg::ref_ptr<SkyBox> sky_box, const std::string& left_image);
	OSG_EDITOR_DLL void skyBoxCreat(osg::ref_ptr<SkyBox> sky_box);

	//IdDistributeVisiotr相关
	OSG_EDITOR_DLL void distributeIdToOsg();
	OSG_EDITOR_DLL void distributeIdToOsgObject(osg::ref_ptr<osg::Object> obj_ptr );
	OSG_EDITOR_DLL void distributeIdToOsgNode(osg::ref_ptr<osg::Node> node_ptr );

	//DOFTransform相关
	OSG_EDITOR_DLL void replaceDOFToMatrixTransform(osg::ref_ptr<osg::Node> node_ptr);

	//Transform相关
	OSG_EDITOR_DLL osg::Matrix transformGetMatrix(osg::ref_ptr<osg::Node> node_ptr);
	OSG_EDITOR_DLL void transformSetMatrix(osg::ref_ptr<osg::Node> node_ptr, osg::Matrix m);

	//PathModel相关
    //OSG_EDITOR_DLL osg::ref_ptr<osg::NodeCallback> pathModelGetAnimationPathCallback(osg::ref_ptr<PathModel> path_model);

	//MatrixTransform相关
	OSG_EDITOR_DLL osg::ref_ptr<osg::MatrixTransform> matrixTransformNew();
    OSG_EDITOR_DLL void matrixTransformEliminateScale(osg::ref_ptr<osg::MatrixTransform> mtPtr);
    OSG_EDITOR_DLL void matrixTransformEliminateRotate(osg::ref_ptr<osg::MatrixTransform> mtPtr);

	//NodeCallBack相关
	OSG_EDITOR_DLL osg::ref_ptr<osg::Object> nodeCallBackPtrToOsgObjectPtr(osg::ref_ptr<osg::NodeCallback> call_back);

	//switch节点相关
	OSG_EDITOR_DLL osg::ref_ptr<osg::Switch> switchNew();
	OSG_EDITOR_DLL void switchSetAllChildrenOn(osg::ref_ptr<osg::Node> node_ptr);
	OSG_EDITOR_DLL void switchSetAllChildrenOff(osg::ref_ptr<osg::Node> node_ptr);

	//临时
	OSG_EDITOR_DLL std::vector<osg::Matrix> matrixVectorNew();
	OSG_EDITOR_DLL void matrixVectorAddMatrix( std::vector<osg::Matrix>& m_v, const osg::Matrix& matrix);
	
	//包围盒相关
	OSG_EDITOR_DLL osg::Vec3 getNodeBoundBoxSize(osg::ref_ptr<osg::Node> node_ptr);
	OSG_EDITOR_DLL osg::Vec3 getNodeBoundBoxCenter(osg::ref_ptr<osg::Node> node_ptr);

	//爆炸图生成
	OSG_EDITOR_DLL void explodedViewGenerate(osg::ref_ptr<osg::Node> node_ptr);

    //find node by name
    OSG_EDITOR_DLL osg::ref_ptr<osg::Node> findNodeByName(const std::string& nodeName);

    //obb生成
    OSG_EDITOR_DLL osg::ref_ptr<osg::Vec3Array> computeObbIgnoreSubSubChildNode(osg::ref_ptr<osg::Node> node_ptr);
    OSG_EDITOR_DLL osg::ref_ptr<osg::Vec3Array> computeObbAxisInWorldFrame(osg::ref_ptr<osg::Node> node_ptr);
}
#endif //!SCENE_VIEWER_FUNCTIONS
